package dao

import (
	"xin-job/admin/dao/model"

	"gorm.io/gorm"
)

func ScheduleJobQuery(maxNextTime int64, pageSize int) (jobs []*model.XxlJobInfo) {
	db.Where("trigger_status = 1 AND trigger_next_time < ?", maxNextTime).Order("ID asc").Limit(pageSize).Find(&jobs)
	return
}

func ScheduleUpdate(job *model.XxlJobInfo) {
	db.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(job).Updates(model.XxlJobInfo{
			TriggerLastTime: job.TriggerLastTime,
			TriggerNextTime: job.TriggerNextTime,
			TriggerStatus:   job.TriggerStatus,
		}).Error; err != nil {
			return err
		}

		return nil
	})
}

func LoadJobInfoById(jobId int) *model.XxlJobInfo {
	var job model.XxlJobInfo
	db.First(&job, jobId)
	return &job
}
